package cn.dylan.service.impl;

import cn.dylan.domain.Product;
import cn.dylan.domain.Store;
import cn.dylan.feignClient.StoreFeignClient;
import cn.dylan.mapper.ProductMapper;
import cn.dylan.service.IProductService;
import cn.dylan.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StoreFeignClient storeFeignClient;

    @Override
    public void save(Product product) throws Exception {
        productMapper.save(product); // 添加后返回自增长id
        Store store = product2store(product);

        JsonResult result = storeFeignClient.handlerAdd(store);

        if(result.getMsg().equals("熔断了")){
            throw new Exception(result.getMsg());
        }

    }

    Store product2store(Product product){
        Store store = new Store();
        store.setProductId(product.getId());
        store.setNumber(product.getNumber());
        /*Product product1 = ProductMapper.loadByName(product.getName());*/
        store.setNumber(product.getNumber());
        return store;
    }
}
